// pages/index/brand/brand.js
var api = require('../../../utils/api.js')
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: null,
    chooseSize: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight
    })
    this.getData()
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res)
        // let statusBarHeight = res.statusBarHeight,
        // navTop = menuButtonObject.top,//胶囊按钮与顶部的距离
        // navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight)*2;//导航高度
        // _that.setData({
        //   navHeight,
        //   navTop
        // })
        // let clientHeight = res.windowHeight;
        // let clientWidth = res.windowWidth;
        // let ratio = 750 / clientWidth;
        // let rpxHeight = clientHeight * ratio;
        // _that.setData({
        //   swiperRPXHeight: rpxHeight - 90 - 65,
        //   screen_width: res.windowWidth / 375,
        // })
      }
    })
  },
  getData(){
    api.get('news').then(res => {
      if (res.code == 200) {
        this.setData({
          content: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  toback() {
    wx.navigateBack()
  },
  tohome() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  hideModal(){
    this.toShow()
  },
  toShow() {
    let _that = this;
    _that.setData({
      chooseSize: !_that.data.chooseSize
    })
  },
  toCollect(e) {
    let res = api.checkLogin()
    if (!res) {
      return;
    }
    let index = e.currentTarget.dataset.index
    let content = this.data.content
    let id = e.currentTarget.dataset.id
    let type_id = e.currentTarget.dataset.type_id
    let obj = {
      type: 30,
      type_id: id
    }
    api.post('favorites', obj).then(res => {
      if (res.code == 200) {
        if (type_id > 0) {
          content[index].type_id=null
          api.tip("取消收藏")
        } else {
          content[index].type_id=1
          api.tip("收藏成功")
        }
        this.setData({
          content
        })
      }
    })
  },
  toDetail(e){
    let index = e.currentTarget.dataset.index
    let url = this.data.content[index].url
    console.log(this.data.content[index].url)
    wx.navigateTo({
      url: './detail/detail?url='+url,
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})